
import D, { MAction, MActionImage } from "./data"

function select<TP, TR>(arr: TP[], map_fun: (param: TP) => TR) {
    let rlist = [];
    for (let i = 0; i < arr.length; i++) {
        let r_item = map_fun(arr[i]);
        rlist.push(r_item);
    }
    return rlist;
}

function textContent_to_tdHtml(text_str: string) {
    let str = text_str.replace(/([。])([^\n])/g, "$1\n$2");
    let list_action = str.split("\n");
    list_action = select(list_action, function (b) {
        return `<span class="action_span">${b}</span>`;
    });
    let action_str = list_action.join("");
    return `<td class="action_td">${action_str}</td>`;
}

function imgs_to_tdHtml(imgs: MActionImage[], dir_path: string) {
    let img_str = "";
    for (let i_img = 0; i_img < imgs.length; i_img++) {
        let img = imgs[i_img];
        let src = ToResourceURL(EResourceID.LibraryOne, `/Projects/囚徒健身示例图/${dir_path}/${img.url}`);
        img_str += `<img src="${src}"/><br/>`;
        if (img.dec) img_str += `<span class="img_span">${img.dec}</span><br/>`;
    }
    return `<td>${img_str}</td>`;
}

/**
 * 悬吊渲染
 * @param min
 * @param max
 * @returns
 */
function ToDailyTrainingHTML_Suspension(min: number, max: number): string {
    min -= 1;
    let name = "";
    let action = "";
    let parse = "";
    let target = "";
    let imgs = "";

    let data_dailyTraining_Suspension: MAction[] = D.GetActionData_DailyTraining_Suspension();
    for (let i_region = min; i_region < max; i_region++) {
        // 悬吊
        let item = data_dailyTraining_Suspension[i_region];
        // 名称
        name += `<td class="name_td">${item.name}</td>`;
        // 描述
        action += textContent_to_tdHtml(item.action);
        // 解析
        parse += textContent_to_tdHtml(item.parse);

        // 训练目标
        let target_str = item.target.join("<br/>");
        target += `<td class="target_td">${target_str}</td>`;

        // 配图
        imgs += imgs_to_tdHtml(item.imgs, "悬吊");
    }
    let table = `
        <div class="bodyBox">
        <table class="tableSuspension">
            <tbody>
                <tr><td class="columnSign">悬吊</td>${name}</tr>
                <tr><td class="columnSign">目标</td>${target}</tr>
                <tr><td class="columnSign">描述</td>${action}</tr>
                <tr><td class="columnSign">配图</td>${imgs}</tr>
                <tr><td class="columnSign">解析</td>${parse}</tr>
            </tbody>
        </table>
        </div>`;
    return table;
}

/**
 * 三决渲染
 * @param type_number
 * @param section_name
 * @returns
 */
function ToDailyTrainingHTML_ThreeSecrets(type_number: number, section_name: string): string {
    let i_table = type_number - 1;
    let name = "";
    let action = "";
    let parse = "";
    let target = "";
    let imgs = "";

    let data_dailyTraining_ThreeSecrets = D.GetActionData_DailyTraining_ThreeSecrets();
    for (let i_region = 0; i_region < 5; i_region++) {
        let item = data_dailyTraining_ThreeSecrets[i_table * 5 + i_region];
        // 名称
        name += `<td class="name_td">${item.name}</td>`;
        // 描述
        if (item.action) action += textContent_to_tdHtml(item.action);
        // 解析
        if (item.progress) parse += textContent_to_tdHtml(item.progress);

        // 训练目标
        let target_str = item.target.join("<br/>");
        target += `<td class="target_td">${target_str}</td>`;

        // 配图
        imgs += imgs_to_tdHtml(item.imgs, "三决");

        // 解析配图
        if (item.progress_imgs) parse += imgs_to_tdHtml(item.progress_imgs, "三决");
    }
    let table = `
        <div class="bodyBox">
        <table>
            <tbody>
                <tr><td class="columnSign">${section_name}</td>${name}</tr>
                <tr><td class="columnSign">目标</td>${target}</tr>
                <tr><td class="columnSign">描述</td>${action}</tr>
                <tr><td class="columnSign">配图</td>${imgs}</tr>
                <tr><td class="columnSign">解析</td>${parse}</tr>
            </tbody>
        </table>
        </div>`;
    return table;
}

/**
 * 训练计划
 * @param min
 * @param max
 * @returns
 */
function ToHTML_TrainingPlan(min: number, max: number): string {
    min -= 1;
    let name = "";
    let action = "";
    let parse = "";
    let target = "";
    let imgs = "";

    let data_TrainingPlan = D.GetActionData_TrainingPlan();
    for (let i_region = min; i_region < max; i_region++) {
        let item = data_TrainingPlan[i_region];
        // 名称
        name += `<td class="name_td">${item.name}</td>`;
        // 描述
        if (item.action) action += textContent_to_tdHtml(item.action);
        // 解析
        if (item.progress) parse += textContent_to_tdHtml(item.progress);

        // 训练目标
        if (item.target) {
            let target_str = item.target.join("<br/>");
            target += `<td class="target_td">${target_str}</td>`;
        }

        // 配图
        imgs += imgs_to_tdHtml(item.imgs, "训练计划");

        // 解析配图
        if (item.progress_imgs) parse += imgs_to_tdHtml(item.progress_imgs, "训练计划");
    }

    let table = `
        <div class="bodyBox">
        <table class="tableTrainingPlan">
            <tbody>
                <tr><td class="columnSign">计划</td>${name}</tr>
                <tr><td class="columnSign">配图</td>${imgs}</tr>
            </tbody>
        </table>
        </div>`;
    return table;
}

/**
 * 传入需要统计哪一个式得数据获取得到六艺表格
 * @param type_number
 * @returns
 */
function ToSixArtsHTML(type_number: number): string {
    // 0-9 俯卧撑
    // 10-19 深蹲
    // 20-29 引体向上
    // 30-39 举腿
    // 40-49 桥
    // 50-59 倒立撑
    let i_table = type_number - 1;
    // 某式
    let tbody = "";
    let name = "";
    let action = "";
    let imgs = "";
    let target = "";
    let parse = "";
    let progress = "";

    let datas_SixArtsTenTypes = D.GetActionData_SixArtsTenTypes();
    for (let i_region = 0; i_region < 6; i_region++) {
        // 某艺的某式
        let item = datas_SixArtsTenTypes[10 * i_region + i_table];
        // 名称
        name += `<td class="name_td">${item.name}</td>`;

        // 训练目标
        let target_str = item.target.join("<br/>");
        target += `<td class="target_td">${target_str}</td>`;

        // 描述
        action += textContent_to_tdHtml(item.action);
        // 解析
        parse += textContent_to_tdHtml(item.parse);
        // 稳扎稳打
        progress += textContent_to_tdHtml(item.progress);

        // 配图
        imgs += imgs_to_tdHtml(item.imgs, "六艺");
    }
    let table = `
        <div class="bodyBox">
        <table>
            <thead>
                <tr>
                    <td>六艺</td>
                    <td>俯卧撑</td>
                    <td>深蹲</td>
                    <td>引体向上</td>
                    <td>举腿</td>
                    <td>桥</td>
                    <td>倒立撑</td>
                </tr>
            </thead>
            <tbody>
                <tr><td class="columnSign">名称</td>${name}</tr>
                <tr><td class="columnSign">目标</td>${target}</tr>
                <tr><td class="columnSign">描述</td>${action}</tr>
                <tr><td class="columnSign">配图</td>${imgs}</tr>
                <tr><td class="columnSign">解析</td>${parse}</tr>
                <tr><td class="columnSign">稳扎稳打</td>${progress}</tr>
            </tbody>
        </table>
        </div>`;
    return table;
}

function ToHTML_TrainingPlan_Customize(): string {
    // 某式
    let tbody = "";
    let name = "";
    let action = "";
    let imgs = "";
    let target = "";
    let parse = "";
    let progress = "";

    let datas_SixArtsTenTypes = D.GetActionData_TrainingPlan_Customize();
    for (let i_region = 0; i_region < datas_SixArtsTenTypes.length; i_region++) {
        let item: MAction = datas_SixArtsTenTypes[i_region];
        // 名称
        name += `<td class="name_td">${item.name}</td>`;

        // 训练目标
        if (item.target) {
            let target_str = item.target.join("<br/>");
            target += `<td class="target_td">${target_str}</td>`;
        }

        // 描述
        if (item.action)
            action += textContent_to_tdHtml(item.action);
        // 解析
        if (item.parse)
            parse += textContent_to_tdHtml(item.parse);
        // 稳扎稳打
        if (item.progress)
            progress += textContent_to_tdHtml(item.progress);

        // 配图
        if (item.imgs)
            imgs += imgs_to_tdHtml(item.imgs, "六艺");
    }
    let table = `
        <div class="bodyBox">
        <table>
            <tbody>
                <tr><td class="columnSign">名称</td>${name}</tr>
                <tr><td class="columnSign">目标</td>${target}</tr>
            </tbody>
        </table>
        </div>`;
    return table;
}

export default {
    ToDailyTrainingHTML_Suspension,
    ToDailyTrainingHTML_ThreeSecrets,
    ToHTML_TrainingPlan,
    ToSixArtsHTML,
    ToHTML_TrainingPlan_Customize,
};
